﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.ViewModel.MiniSite;
using YunSpace.Domain.IRepository.MiniSite;
using YunSpace.Repository.MiniSite;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;

#endregion

namespace YunSpace.Application.MiniSite
{
    public class CompanionSettingApp
    {
        private ICompanionSettingRepository service = new CompanionSettingRepository();

        public List<CompanionSettingEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<CompanionSettingEntity>();
            var queryParam = queryJson.ToJObject();
            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public CompanionSettingEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(CompanionSettingEntity companionsettingEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                companionsettingEntity.Modify(keyValue);
                service.Update(companionsettingEntity);
            }
            else
            {
                companionsettingEntity.Create();
                // companionsettingEntity.F_Category = 2;
                service.Insert(companionsettingEntity);
            }
        }
        /// <summary>
        /// 根据空间ID获取对应的空间配套设施
        /// </summary>
        /// <param name="spaceId"></param>
        /// <param name="siteId"></param>
        /// <returns></returns>
        public List<CompanionSettingModel> GetCommpanionBySpaceId(string spaceId,string siteId)
        {
            using (var db = new RepositoryBase())
            {
                var strSql = @"select distinct mc.*,items.F_ItemName,items.F_Description from MiniSite_CompanionSetting mc inner join Sys_ItemsDetail items  on mc.f_companionname=items.F_ItemCode 
                            inner join Sys_Items i on i.F_Id=items.F_ItemId and i.F_EnCode='SiteSupportingFacilities' where 1=1 {0}";
                if (!spaceId.IsNullOrEmpty()) {
                    strSql = string.Format(strSql, " AND  mc.f_spaceId=@f_spaceId");
                }
                else if (!siteId.IsNullOrEmpty())
                {
                    strSql = string.Format(strSql, " AND  mc.f_siteId=@f_siteId");
                }
                DbParameter[] parameter =
                {
                    new MySqlParameter("@f_spaceId", spaceId),
                    new MySqlParameter("@f_siteId",siteId)
                };
                return db.FindList<CompanionSettingModel>(strSql, parameter);
            }
        }
    }
}

